// JavaScript Document
var CookiesHelper = function() {
	var d = new Date()
	d.setDate(d.getDate() + 15);
	this.expriceDate = d.toUTCString();             //设置过期时间
	this.unDesCodeValue = document.cookie;          //未解码的Cookies值
	this.cookies = new Array();  //cookies对象
	if (this.unDesCodeValue != '' && this.unDesCodeValue != null) {
		var tempArr = this.unDesCodeValue.split(';'); //将cookies对象分开
		for (var i = 0; i < tempArr.length; i++) {
			var arrCookItem;
			if (tempArr[i].split('=').length > 2){        //如果cookie项有子cookies值,暂时不支持写法Response.Cookies["Name"]["Key"]
			//                arrCookItem = new Array();
			//                arrCookItem.push(tempArr[i].substring(0, tempArr[i].indexOf('=') - 1));
			//                arrCookItem.push(tempArr[i].substring(tempArr[i].indexOf('='), tempArr[i.length-1]).split('&'))
			//                arrCookItem[0] = decodeURIComponent(arrCookItem[0]);  //解码名称和值
			}
			else {
				arrCookItem = tempArr[i].split('=');   //将cookies名称和值分开
				arrCookItem[0] = decodeURIComponent(arrCookItem[0]);  //解码名称和值
				arrCookItem[1] = decodeURIComponent(arrCookItem[1]);
				this.cookies.push(arrCookItem);        //得到一个二维数组，对应cookies值
			}
		}
	}
	this.getCookies=function(key){
		if (null == this.cookies) return '';
		var name;
		var value;
		var arrTemp;
		for (var i = 0; i < this.cookies.length; i++) {
			arrTemp = this.cookies[i];
			name = arrTemp[0];
			value = arrTemp[1];
			if (name == key){
				return value;
			}
		}
		return null;
	}
	this.setCookies=function(key,newvalue){
		var name;
		var value;
		var arrTemp;
		var isAdd = true;  //判断是否是添加新Cookies值
		for (var i = 0; i < this.cookies.length; i++) {
			arrTemp = this.cookies[i];
			name = arrTemp[0];
			value = arrTemp[1];
			if (name == key) {
				this.cookies[i][1] = newvalue;     //如果有则修改；
				document.cookie = key + '=' + encodeURIComponent(newvalue) + "; expires=" + this.expriceDate + "";
				isAdd = false;
				break;
			}
		}
		if (isAdd) {
			var newCookie = new Array();            //如果没有则添加;
			newCookie.push(key);
			newCookie.push(value);
			this.cookies.push(newCookie);
			document.cookie = key + '=' + encodeURIComponent(value) + "; expires=" + this.expriceDate + "";
			alert(document.cookie);
		}
	}
	this.deleteCookies=function(key){
		if (this.cookies.length == 0){
			return;
		}else{
			var d = new Date();
			d.setDate(d.getDate() - 1);
			var realCookies = '';
			var documentCookie = new Array();       //删除时要更新数组
			for (var i = 0; i < this.cookies.length; i++) {
				arrTemp = this.cookies[i];
				name = arrTemp[0];
				value = arrTemp[1];
				if (name.replace(/\s/g,'') == (key)) {
					realCookies += name + '=' + encodeURIComponent(value) + "; expires=" + d.toGMTString() + "";
					break;
				}else{
					var tempArr = new Array();
					tempArr.push(name);
					tempArr.push(value);
					documentCookie.push(tempArr);
				}
			}
			this.cookies = documentCookie;
			document.cookie = realCookies;
		}
	}
		
}
var cookieObj = new CookiesHelper();
